package com.example.houtai.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.houtai.entity.SysUser;
import com.example.houtai.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * @author wgq
 */
@RestController
@RequestMapping("/sys/suser")
@Slf4j
public class UserController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ISysUserService iSysUserService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @GetMapping("/info")
    public Principal user(Principal member) {
        log.info("===" + member);
        return member;
    }

    @PostMapping("/saveredis")
    public void saveRedis(@RequestBody SysUser user){

        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("username", user.getUsername());
//        sysUserQueryWrapper.eq("password", passwordEncoder.encode(user.getPassword()));
        SysUser one = iSysUserService.getOne(sysUserQueryWrapper);
        String s = JSONObject.toJSONString(one);

        String key = "SysUser";
        if (redisTemplate.hasKey(key)){
            redisTemplate.delete(key);
            redisTemplate.opsForValue().set(key, s);
        } else {
            redisTemplate.opsForValue().set(key, s);
        }

    }

}
